package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.CheckGroupService;
import com.itheima.service.UserMannagementService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.MessageDigest;
import java.util.Base64;
import java.util.List;

/**
 * 检查组管理
 */
@RestController
@RequestMapping("/usermanagement")
public class UserMannagementController {
    @Reference
    private UserMannagementService userMannagementService;

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody User user, Integer[] roleIds) {
            String username = user.getUsername();
        long count = userMannagementService.findUserByUsername(username);
        if (count>0){
            return new Result(false,"会员名重复");
        }else {
            try {
                String password = user.getPassword();
                if (password != null && password.length() > 0) {
                    //获取MD5
                    MessageDigest md5 = MessageDigest.getInstance("md5");
                    //生成随机密码 不可逆
                    byte[] bytes = md5.digest(password.getBytes());
                    String str = Base64.getEncoder().encodeToString(bytes);
                    user.setPassword(str);
                }
                userMannagementService.add(user, roleIds);

                return new Result(true, MessageConstant.ADD_USER_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, MessageConstant.ADD_USRER_FAIL);
            }
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean) {
        return userMannagementService.findPage(pageBean);
    }

    //根据id查询用户
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            User user = userMannagementService.findById(id);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FALL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds) {
        try {
            userMannagementService.edit(user, roleIds);
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
    }

    //删除用户
    @RequestMapping("/deleteByUserId")
    public Result deleteByUserId(Integer userId) {
        try {
            userMannagementService.deleteByUserId(userId);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

    //查询所有用户
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<User> list = userMannagementService.findAll();
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FALL);
        }
    }

    //查询所有检查项
    @RequestMapping("/roleFindAll")
    public Result roleFindAll() {
        try {
            List<Role> list = userMannagementService.roleFindAll();//发送请求
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FALL);
        }

    }

    //根据检查组id查询关联的检查项id
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer userId){
        try{
            List<Integer> list = userMannagementService.findRoleIdsByUserId(userId);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FALL);
        }
    }
}
